package com.kyh.util;

import org.apache.commons.io.FileUtils;

import com.kyh.util.MD5Util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

    /**
     * 使用gzip进行压缩
     */
    public static String gzip(String primStr) {
        if (primStr == null || primStr.length() == 0) {
            return primStr;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(primStr.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (gzip != null) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return new sun.misc.BASE64Encoder().encode(out.toByteArray());
    }

    /**
     * <p>Description:使用gzip进行解压缩</p>
     *
     * @param compressedStr
     * @return
     */
    public static String gunzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        GZIPInputStream ginzip = null;
        byte[] compressed = null;
        String decompressed = null;
        try {
            compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            in = new ByteArrayInputStream(compressed);
            ginzip = new GZIPInputStream(in);

            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }

        return decompressed;
    }

    /**
     * 使用zip进行压缩
     *
     * @param str 压缩前的文本
     * @return 返回压缩后的文本
     */
    public static final String zip(String str) {
        if (str == null)
            return null;
        byte[] compressed;
        ByteArrayOutputStream out = null;
        ZipOutputStream zout = null;
        String compressedStr = null;
        try {
            out = new ByteArrayOutputStream();
            zout = new ZipOutputStream(out);
            zout.putNextEntry(new ZipEntry("0"));
            zout.write(str.getBytes());
            zout.closeEntry();
            compressed = out.toByteArray();
            compressedStr = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
        } catch (IOException e) {
            compressed = null;
        } finally {
            if (zout != null) {
                try {
                    zout.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return compressedStr;
    }

    /**
     * 使用zip进行解压缩
     *
     * @param compressedStr 压缩后的文本
     * @return 解压后的字符串
     */
    public static final String unzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        String decompressed = null;
        try {
            byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressed);
            zin = new ZipInputStream(in);
            zin.getNextEntry();
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            decompressed = null;
        } finally {
            if (zin != null) {
                try {
                    zin.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return decompressed;
    }
    
 // 压缩
    public static String compress(String str) throws IOException {
      if (str == null || str.length() == 0) {
        return str;
      }
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      GZIPOutputStream gzip = new GZIPOutputStream(out);
      gzip.write(str.getBytes());
      gzip.close();
      return out.toString("ISO-8859-1");
    }
   
    // 解压缩
    public static String uncompress(String str) throws IOException {
      if (str == null || str.length() == 0) {
        return str;
      }
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      ByteArrayInputStream in = new ByteArrayInputStream(str
          .getBytes("ISO-8859-1"));
      GZIPInputStream gunzip = new GZIPInputStream(in);
      byte[] buffer = new byte[256];
      int n;
      while ((n = gunzip.read(buffer)) >= 0) {
        out.write(buffer, 0, n);
      }
      // toString()使用平台默认编码，也可以显式的指定如toString("GBK")
      return out.toString();
    }
    
    /**
     * 短网址生成方法
     * 这个方法会,生成四个短字符串,每一个字符串的长度为6
     * 这个方法是从网上搜索的一个方法,但不知道出自何处了,稍微将key换了一下
     * @param url
     * @return
     */
    public static String[] shortUrl(String url) {

        // 可以自定义生成 MD5 加密字符传前的混合 KEY
        String key = UUID.randomUUID().toString();
        
        //混淆key,加上当前时间,并且取一个随机字符串
        key = System.currentTimeMillis() + key;
        // 要使用生成 URL 的字符

        String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h",

                "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",

                "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",

                "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",

                "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",

                "U", "V", "W", "X", "Y", "Z"
        };

        // 对传入网址进行 MD5 加密
        String sMD5EncryptResult = MD5Util.encodeByMd5(key + url);

        String hex = sMD5EncryptResult;

        String[] resUrl = new String[4];

        for (int i = 0; i < 4; i++) {

            // 把加密字符按照 8 位一组 16 进制与 0x3FFFFFFF 进行位与运算

            String sTempSubString = hex.substring(i * 8, i * 8 + 8);

            // 这里需要使用 long 型来转换，因为 Inteper .parseInt() 只能处理 31 位 , 首位为符号位 , 如果不用
            // long ，则会越界

            long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16);

            String outChars = "";

            for (int j = 0; j < 6; j++) {

                // 把得到的值与 0x0000003D 进行位与运算，取得字符数组 chars 索引

                long index = 0x0000003D & lHexLong;

                // 把取得的字符相加

                outChars += chars[(int) index];

                // 每次循环按位右移 5 位

                lHexLong = lHexLong >> 5;

            }

            // 把字符串存入对应索引的输出数组

            resUrl[i] = outChars;

        }
        
        return resUrl;
    }

    /**
     * 获取我想要的字符串,将生成的两个相加,得到我想要的12位字符
     * @param url
     * @return
     */
    public static String getShortUrl(String url){
        String[] aResult = shortUrl(url);
        
        return aResult[0] + aResult[1];
    }
    
    //测试方法
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0;i<1000;i++){
            sb.append("ab");//长度为2000的一个字符串
        }
        
        String[] strings = shortUrl(sb.toString());
        for(int i = 0;i<strings.length;i++){
            System.out.println(strings[i]);
        }
        
        //验证生成的是否是唯一的
        List<String> list = new ArrayList<>();
        //生成1000万个短字符,已经足够使用
        for (int i = 0; i<1000; i++){
            list.add(getShortUrl(sb.toString()));
        }
        
        //经测试生成1000万个短链接没有重复的
        //这里的判断重复的方法,只使用一次循环
        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
        for (String string : list) {
            if (hashMap.get(string) != null) {
                Integer value = hashMap.get(string);
                hashMap.put(string, value+1);
                System.out.println("the element:"+string+" is repeat");
            } else {
                hashMap.put(string, 1);
            }
        }
    }
    
    

//    public static void main(String[] args) throws IOException {
//       String strOld=FileUtils.readFileToString(new File("D:/test.txt"),"utf-8");
//        System.out.println(strOld.length());
//        String gzip = zip(strOld);
//        System.out.println(gzip.length());
//    }
}